package com.fitso.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.fitso.model.bean.journal.FoodJournal;

@Controller
public class JournalController {
	Logger logger = Logger.getLogger(JournalController.class);

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	// TODO: Use REST styled @PathVariable when Spring fixes the defect
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView dated(@RequestParam(value = "date", required = false) Date date) {
		logger.info("date is: " + date);
		return new ModelAndView("/journals/show", "journal", new FoodJournal());
	}
}
